package com.zc.linkedlist;

import org.junit.Test;

public class LinkedList {
	class Node{
		int value;
		Node next=null;
		public Node(int value) {
			this.value=value;
		}
	}
	
	Node head=null;
	
	public LinkedList(int[] arr) {
		if(arr==null||arr.length<=0) {
			return;
		}
			
		Node pre=new Node(0);
		Node iter=pre;
		
		for(int i:arr) {
			iter.next=new Node(i);
			iter=iter.next;
		}
		
		head=pre.next;
	}
	
	public void printLinkedList() {
		Node iter=head;
		while(iter!=null) {
			System.out.println(iter.value);
			iter=iter.next;
		}
	}
	
	public void reverseLinkedList() {
		if(head==null) {
			return;
		}
		
		Node first=head;
		Node second=head.next;
		head.next=null;
		
		while(second!=null) {
			Node third=second.next;
			second.next=first;
			
			first=second;
			second=third;
		}
		
		head=first;
		
		return;
	}
	
	@Test
	public void test() {
		
	}
	
	public static void main(String[] args) {
		int[] arr= {2,4,6,1,8};
		LinkedList list=new LinkedList(arr);
		list.printLinkedList();
		list.reverseLinkedList();
		list.printLinkedList();
	}
	
}
